package com.archermind.boc.dal.business.file.impl;

import java.io.File;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import com.archermind.boc.Logger;
import com.archermind.boc.dal.business.file.IZhiWenHelper;
import com.archermind.boc.xml.ApplicationContextFactory;
import com.archermind.boc.xml.parser.impl.XMLParser;

public class ZhiWenHelper implements IZhiWenHelper {

	private static Object lock = new Object();
	private final static Logger logger = Logger.getLogger(XMLParser.class
			.getName());
	private String rootPath = "C:/zhiWen";
	private static IZhiWenHelper zhiWenHelper;
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

	private ZhiWenHelper() {
	}

	public static IZhiWenHelper getInstance() {
		synchronized (lock) {
			if (zhiWenHelper == null)
				zhiWenHelper = (IZhiWenHelper) ApplicationContextFactory
						.getContext().getBean("zhiWenHelper");
		}
		return zhiWenHelper;
	}

	public String getRootPath() {
		return rootPath;
	}

	public void setRootPath(String rootPath) {
		this.rootPath = rootPath;
	}

	public void delFile(String path) {
		// TODO Auto-generated method stub
		File file = getFile(path);
		if (file.exists())
			file.delete();
	}

	public void delFilesByDate(Date date) {
		// TODO Auto-generated method stub

	}

	public void delFilesByID(Date date) {
		// TODO Auto-generated method stub

	}

	public boolean exists(String path) {
		// TODO Auto-generated method stub
		return getFile(path).exists();
	}

	public File getFile(String path) {
		return new File(rootPath + "/" + path);
	}

	private String getRelativePath(String date, String sessionID) {
		return date + "/" + sessionID;
	}

	private String getRelativePath(Date date, String sessionID) {
		String strDate = null;
		strDate = dateFormat.format(date);
		return getRelativePath(strDate, sessionID);
	}

	public byte[] read(String path) {
		byte[] b = null;
		File file = getFile(path);
		if (file.exists()) {
			int length = 0;
			FileInputStream in = null;
			try {
				in = new FileInputStream(file);
				length = in.available();
				byte[] tmpByte = new byte[length];
				in.read(tmpByte);
				b = tmpByte;
			} catch (FileNotFoundException e) {
				logger.error("file not find!", e);
				return b;
			} catch (IOException e) {
				logger.error(e.getMessage());
				return b;
			} finally {
				if (in != null)
					try {
						in.close();
					} catch (IOException e) {
						logger.error("error when close file input!", e);
					}
			}
		}
		return b;
	}

	public String write(String data, String sessionID) {
		// TODO Auto-generated method stub
		return write(data, new Date(), sessionID);
	}

	public String write(String data, Date date, String sessionID) {
		String folderRelativePath = getRelativePath(date, sessionID);
		String fileRelativePath = folderRelativePath + "/" + UUID.randomUUID()
				+ ".txt";
		File file = null;
		FileOutputStream out = null;
		try {
			file = createFile(fileRelativePath);
			out = new FileOutputStream(file);
			out.write(data.getBytes());
		} catch (FileNotFoundException e) {
			logger.error("error when write file!", e);
			fileRelativePath = null;
		} catch (IOException e) {
			logger.error("error when write file!", e);
			fileRelativePath = null;
		} finally {
			if (out != null)
				try {
					out.close();
				} catch (IOException e) {
					logger.error("error when close outputstream!", e);
				}
		}
		return fileRelativePath;
	}

	private File createFile(String folderRelativePath) throws IOException {
		File file = getFile(folderRelativePath);
		File parent = file.getParentFile();
		logger.info(parent.toString());
		File pparent = parent.getParentFile();
		logger.info(pparent.toString());
		if (!pparent.exists())
			pparent.mkdir();
		if (!parent.exists())
			parent.mkdir();
		System.out.println("===========" + file.getPath());
		file.createNewFile();
		return file;
	}
}
